package com.itheima.controller;

import com.itheima.domain.PmsProductCategory;
import com.itheima.manager.PmsProductCategoryManager;
import com.itheima.util.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;


@RestController
@RequestMapping("/productCategory")
public class PmsProductCategoryController {

    @Autowired
    PmsProductCategoryManager pmsProductCategoryManager;

    //查询所有一级分类及子分类
    @GetMapping("/list/withChildren")
    public R withChildren() {
        return pmsProductCategoryManager.withChildren();
    }

    //添加商品分类
    @PostMapping("/create")
    public R create(@RequestBody PmsProductCategory pmsProductCategory) {
        return pmsProductCategoryManager.create(pmsProductCategory);
    }

    //查询产品分类
    @GetMapping("/list/{parentId}")
    public R findByPage(
            @PathVariable Long parentId,
            @RequestParam(required = false, defaultValue = "1") Integer pageNum,
            @RequestParam(required = false, defaultValue = "10") Integer pageSize) {
        return pmsProductCategoryManager.findByPage(pageNum, pageSize, parentId);
    }

    //根据id获取商品分类
    @GetMapping("/{id}")
    public R findById(@PathVariable Long id) {
        return pmsProductCategoryManager.findById(id);
    }

    //修改导航栏显示状态
    @PostMapping("/update/navStatus")
    public R navStatus(@RequestParam Long[] ids, @RequestParam Integer navStatus) {
        pmsProductCategoryManager.navStatus(ids, navStatus);
        return R.success("操作成功");
    }

    //修改显示状态
    @PostMapping("/update/showStatus")
    public R showStatus(@RequestParam Long[] ids, @RequestParam Integer showStatus) {
        pmsProductCategoryManager.showStatus(ids, showStatus);
        return R.success("操作成功");
    }

    //删除商品分类
    @PostMapping("/delete/{id}")
    public R delete(@PathVariable Long id) {
        pmsProductCategoryManager.delete(id);
        return R.success("删除成功");
    }

    //修改商品分类
    @PostMapping("/update/{id}")
    public R update(@PathVariable Long id, @RequestBody PmsProductCategory pmsProductCategory) {
        pmsProductCategoryManager.update(id, pmsProductCategory);
        return R.success("修改成功");
    }
}
